#include <stdio.h> 
#ifdef unix
#include <sys/types.h> 
#include <unistd.h> 
#include <signal.h>
#endif
#include "QThread.h"
#include<errno.h>

void* tPro(void* pars) 
{ 
    QThread* p1; 
    p1=(QThread*)pars; 
#ifdef _WIN32
    pthread_t nTid = pthread_self(); 
#else
	int nTid =pthread_self();
#endif
    p1->setTid(nTid); 
    p1->run();     
    return NULL; 
} 

QThread::~QThread()
{
}
void QThread::start(void) 
{ 
    pthread_create(&m_thd,NULL,tPro,(void*)this); 
} 
 
 
#ifdef _WIN32
void QThread::setTid(pthread_t nTid) 
{ 
    m_nTid = nTid; 
} 

pthread_t QThread::getTid(void)
{
	return m_nTid;
}

#else
void QThread::setTid(int nTid) 
{ 
    m_nTid = nTid; 
} 

int QThread::getTid(void) 
{ 
    return m_nTid; 
} 
 
#endif

 

//judge if thread is running
bool QThread::isRunning(void) 
{ 
    int nRet = pthread_kill(m_nTid,0); 
    if(ESRCH==nRet)  //not exist
    { 
		return false; 
	} 
	else if(EINVAL==nRet) //illegal signal
	{
		return false;
	}
    else 
    { 
		return true; 
	} 
}
